package org.eae.eldf.webapp.controller.sys;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.eae.eldf.dto.sys.SysParameterQueryDto;
import org.eae.eldf.entity.sys.SysParameter;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.sys.SysParameterService;

@Controller
public class SysParameterController extends AbstractController<SysParameter>{

	
	private SysParameterService sParameterService;
    @Autowired
	public void setsParameterService(SysParameterService sParameterService) {
		this.sParameterService = sParameterService;
	}
    
   /**
    * 查询列表 sysParameter
    * @param searchValue
    * @param map
    * @param request
    * @param response
    * @param username
    * @return
    */
    @RequestMapping(value="/sys/parameter/list")
    public String initPage(
    		ModelMap map,HttpServletRequest request,
    		HttpServletResponse response){
    	
		return "/sys/parameter/sys_parameter";
    }
    
    
    @RequestMapping(value="/sys/parameter/list_json")
    @ResponseBody
    public ResultList<SysParameter>pageList(
    		@ModelAttribute(value = "sysParameterQueryDto") SysParameterQueryDto sysParameterQueryDto,
    		ModelMap map,HttpServletRequest request,
    		HttpServletResponse response){
    	Pagination<SysParameter> pagination=this.newPaginationInstance(sysParameterQueryDto);
    	pagination=sParameterService.getAllParams(pagination, sysParameterQueryDto);
		return outPageArrayList(pagination, sysParameterQueryDto);
    }
    /**
     * 跳转到系统参数设置页面
     * @param id
     * @param modelMap
     * @param username
     * @param searchValue
     * @return
     */
    @RequestMapping("/sys/parameter/form")
    @Token(save=true)
    public String toParamsForm(@RequestParam(required=false) Long id,
    		ModelMap modelMap,
    		@RequestParam(required=false) String searchValue){
    	SysParameter sysParameter=null;
    	if(null!=id){
    		sysParameter=sParameterService.findByParamsId(id);
    	}
    	else {
			sysParameter=new SysParameter();
		}
    	modelMap.addAttribute("sysParameter",sysParameter);
    	modelMap.addAttribute("searchValue",searchValue);
		return "/sys/parameter/form";
    }
    
    /**
     * 新增系统参数
     * @param parametername
     * @param parametervalue
     * @param parametercategory
     * @param markstatus
     * @param note1
     * @return
     */
    @RequestMapping(value="/sys/parameter/addparams",method=RequestMethod.POST)
   /* @Token(remove=true,repeatSubmitForwardURL="/sys/parameter/form")*/
    @ResponseBody
    public Result insert(
    		@ModelAttribute(value="sysParameter" )SysParameter sysParameter,
    		HttpServletRequest request,
    		ModelAndView modelAndView,ModelMap modelMap){
			Result result = new Result(StateDefine.FLAG_SUCCESS, true);
			/*modelAndView.setViewName("forward:/sys/parameter/form");*/
			//查询参数名是否存在 若存在 提示"参数名已经存在"
			if(sParameterService.queryCount(Criteria.create(SysParameter.class).where("parametername", new Object[]{sysParameter.getParametername()}))>0){
				RequestContext context=new RequestContext(request);
				modelMap.addAttribute("errorMsg",context.getMessage("param.msg.sparamname.exist"));
				modelAndView.addObject("success",false);
			}
			else{
				sysParameter.setParametername(sysParameter.getParametername());
				sysParameter.setParametervalue(sysParameter.getParametervalue());
				sysParameter.setParametercategory(sysParameter.getParametercategory());
				sysParameter.setMarkstatus(sysParameter.getMarkstatus());
				sysParameter.setLastupdatetime(new Date());
				sysParameter.setLastupdateusername(SpringSecurityUtils.getCurrentUserName());
				sysParameter.setNote1(sysParameter.getNote1());
				sysParameter.setIsencrypt(0);
				sysParameter.setNote2(null);
				sysParameter.setNote3(null);
				sParameterService.insert(sysParameter);
				modelAndView.addObject("success",true);
			}
    	     return result;
    }
    
    /**
     * 编辑系统参数
     * @param id
     * @param parametername
     * @param parametervalue
     * @param parametercategory
     * @param markstatus
     * @param note1
     * @param username
     * @param modelAndView
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/sys/parameter/editparams",method=RequestMethod.POST)
    public Result update(
    		@ModelAttribute(value="sysParameter" )SysParameter sysParameter,
    		ModelAndView modelAndView,ModelMap modelMap){
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
    	sParameterService.update(Criteria.create(SysParameter.class)
    			.set("parametervalue", sysParameter.getParametervalue())
    			.set("parametercategory", sysParameter.getParametercategory())
    			.set("markstatus", sysParameter.getMarkstatus())
    			.set("note1", sysParameter.getNote1())
    			.where("sysParameterId",new Object[]{sysParameter.getSysParameterId()}));
    	modelAndView.addObject("success",true);
		return result;
    	
    }
    
    /**
     * 删除系统参数
     * @param id
     * @param username
     * @return
     */
    @RequestMapping(value="/sys/parameter/deleteparams")
    public @ResponseBody Result delete(@ModelAttribute(value="sysParameter" )SysParameter sysParameter){
    	Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		sParameterService.delete(sysParameter.getSysParameterId());
    	
    	return result;
    	
    }
}
